package config

import (
	"fmt"

	"github.com/cihub/seelog"
)

const seelogConfig = `
<seelog levels="trace,debug,info,warn,error,critical">
    <outputs formatid="main">
        <filter levels="trace,debug,info">
            <console formatid="colored-default"/>
            <!-- 将日志输出到磁盘文件，按文件大小进行切割日志，日志文件路径相对于启动文件，单个文件最大maxsizeM，最多maxrolls个日志文件 -->
            <rollingfile formatid="main" type="size" filename="log/info.log" maxsize="10485760" maxrolls="10" />
        </filter>
        <filter levels="warn">
            <console formatid="colored-warn"/>
            <rollingfile formatid="main" type="size" filename="log/warn.log" maxsize="10485760" maxrolls="10" />
        </filter>
        <filter levels="error,critical">
            <console formatid="colored-error"/>
            <rollingfile formatid="main" type="size" filename="log/error.log" maxsize="10485760" maxrolls="10" />
        </filter>
    </outputs>
    <formats>
        <format id="colored-default"  format="[%LEVEL] %EscM(38)%Date %Time %File:%Line: %Msg%n%EscM(0)"/>
        <format id="colored-warn"  format="[%LEVEL] %EscM(33)%Date %Time %File:%Line: %Msg%n%EscM(0)"/>
        <format id="colored-error"  format="[%LEVEL] %EscM(31)%Date %Time %File:%Line: %Msg%n%EscM(0)"/>
        <format id="main" format="[%LEVEL] %Date %Time %File:%Line: %Msg%n"/>
    </formats>
</seelog>
`

// InitSeelog 配置seelog框架
func InitSeelog() {
	logger, err := seelog.LoggerFromConfigAsString(seelogConfig)
	if err != nil {
		fmt.Errorf("fail to load loggerConfig, err:%v", err)
	} else {
		seelog.ReplaceLogger(logger)
	}
}
